<template>
    <div class="headerBgColor">
        <el-form
            ref="formRef"
            inline
            :model="state.formValue"
            label-placement="left"
            label-align="right"
            :show-feedback="false"
            require-mark-placement="left"
        >
            <el-form-item>
                <template v-for="item in props.itemFrom" :key="item.name">
                    <el-form-item :label="item.label">
                        <!-- 普通input -->
                        <el-input
                            v-if="item.type === 'input'"
                            v-model="state.formValue[item.modeVal]"
                            :placeholder="item.placeholder"
                            :style="{ width: item.width }"
                        />
                        <!-- 下拉框 -->
                        <el-select
                            v-if="item.type === 'select'"
                            v-model="state.formValue[item.modeVal]"
                            :placeholder="item.placeholder"
                            :style="{ width: item.width }"
                        >
                            <el-option
                                v-for="items in item.options"
                                :key="items.value"
                                :label="items.label"
                                :value="items.value"
                            />
                        </el-select>

                        <!-- 时间范围 -->
                        <el-date-picker
                            v-if="item.type === 'datePick'"
                            v-model="state.formValue[item.modeVal]"
                            prefix-icon=""
                            type="datetimerange"
                            :style="{ width: item.width }"
                            value-format="YYYY-MM-DD hh:mm:ss"
                            clearable
                        />
                    </el-form-item>
                </template>
            </el-form-item>
            <el-form-item>
                <el-button :icon="RefreshLeft" @click="restClick">重置</el-button>

                <el-button type="primary" :icon="Search" style="color: #fff" @click="handleValidateClick">
                    查询
                </el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup lang="ts">
import { reactive, watch, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { Search, RefreshLeft } from '@element-plus/icons-vue';
import { HeaderFromOption } from '@/interface';

interface Props {
    itemFrom: HeaderFromOption;
}

const emit = defineEmits<{
    (e: 'getFormData', data: number | object): void;
}>();

const props = withDefaults(defineProps<Props>(), {
    itemFrom: () => []
});
const formRef = ref<FormInstance>();
const state = reactive({
    formValue: {} as any
});

watch(
    () => props.itemFrom,
    (val: any) => {
        const o: { [key in string]: any } = {};
        if (val.length) {
            for (const item of val) {
                o[item.modeVal] = '';
            }
        }
        state.formValue = o;
    },
    {
        immediate: true
    }
);
/** 暴露值，传递给父组件 */
const emitFormData = (data: any) => emit('getFormData', data);
// 查询
const handleValidateClick = () => {
    const setObj: { [key in string]: any } = {};
    for (const item in state.formValue) {
        if (state.formValue[item]) {
            setObj[item] = state.formValue[item];
        }
    }
    emitFormData(setObj);
};
// 重置
const restClick = () => {
    Object.keys(state.formValue).forEach((key: any) => {
        state.formValue[key] = '';
    });
    emitFormData(state.formValue);
};
</script>

<style scoped lang="scss"></style>
